// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// U [author] rhyme_lph
// u [email]  rhymelph@gmail.com
// S [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/company_entity.dart';
import 'package:finance_app/src/entities/payment_method_entity.dart';
import 'package:finance_app/src/entities/plans_item_entity.dart';
import 'package:finance_app/src/entities/promo_code_entity.dart';
import 'package:finance_app/src/entities/sub_cards_entity.dart';
import 'package:finance_app/src/provider/setting/subscription/sub_payment_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/sub_price_form.dart';
import 'package:finance_app/src/widgets/form/value_shadow_select_form.dart';
import 'package:finance_app/src/widgets/input/input_name_field.dart';
import 'package:finance_app/src/widgets/input/phone_number_input_field.dart';
import 'package:finance_app/src/widgets/list_item/pay_bill_item.dart';
import 'package:finance_app/src/widgets/list_item/sub_billing_header_item.dart';
import 'package:finance_app/src/widgets/list_item/sub_company_billing_item.dart';
import 'package:finance_app/src/widgets/list_item/sub_promo_code_item.dart';
import 'package:finance_app/src/widgets/pop/normal_border_pop.dart';
import 'package:flutter/material.dart';

export 'package:finance_app/src/entities/plans_item_entity.dart';

@RouterMeta(paramName: "subPaymentPage")
class SubPaymentPage extends StatefulWidget {
  final PlansItemEntity plan;
  final int billId;
  final int companyId;
  final String companyName;

  const SubPaymentPage({
    Key key,
    this.plan,
    this.billId,
    this.companyId,
    this.companyName,
  }) : super(key: key);

  @override
  _SubPaymentPageState createState() => _SubPaymentPageState();
}

class _SubPaymentPageState extends State<SubPaymentPage>
    with PagePresenter<SubPaymentPage, SubPaymentProvider> {
  @override
  void initState() {
    provider.plan = widget.plan;
    provider.billId = widget.billId;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.billing,
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          padding: EdgeInsets.only(top: getDp(20)),
          children: [
            SubPaymentItem(children: [
              SpaceBetweenItem(
                  title: S.current.company,
                  value: widget.companyName != null
                      ? widget.companyName
                      : Config.currentCompany.name),
              SpaceBetweenItem(
                  title: S.current.billing_period,
                  value: provider.billingPeriod),
            ]),
            AppSpaces.h20,
            Padding(
              padding: EdgeInsets.only(
                left: getDp(25),
                right: getDp(28),
              ),
              child: SubBillingHeaderItem(
                title: S.current.review_payment,
              ),
            ),
            AppSpaces.h20,
            listBuilder<PromoCodeEntity>(
              selector: (_, __) => __.promoCodeEntity,
              builder:
                  (BuildContext context, PromoCodeEntity value, Widget child) =>
                      Visibility(
                visible: value != null,
                child: Padding(
                  padding: EdgeInsets.only(
                    left: getDp(25),
                    right: getDp(28),
                    bottom: getDp(20),
                  ),
                  child: SubPromoCodeItem(
                    data: value,
                  ),
                ),
              ),
            ),
            SubPaymentItem(children: [
              SpaceBetweenItem(
                  title: widget.plan.nameText,
                  value: '\$' + (widget.plan.price)),
              if (widget.plan.upgradePrice != null &&
                  widget.plan.upgradePrice != widget.plan.price)
                Column(
                  children: [
                    SizedBox(height: 2),
                    SpaceBetweenItem(
                      title: S.current.refund,
                      value: '-\$' +
                          ((num.tryParse(widget.plan.price) -
                                  (num.tryParse(widget.plan.upgradePrice))))
                              .toStringAsFixed(2),
                    ),
                  ],
                ),
              AppSpaces.h10,
              listBuilder<PromoCodeEntity>(
                  selector: (_, __) => __.promoCodeEntity,
                  builder: (BuildContext context, PromoCodeEntity value,
                          Widget child) =>
                      value == null
                          ? PressedOpacityButton(
                              child: Text(
                                S.current.have_promo_code,
                                style: TextStyle(
                                  color: aceBlueColor,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                              onPressed: provider.onAddPromoCode,
                            )
                          : GestureDetector(
                              onTap: provider.onAddPromoCode,
                              child: Column(
                                children: [
                                  SubPriceForm(
                                    title: S.current.promo_code,
                                    amount: '-\$' +
                                        CashUtils.formatCashAmount(
                                            value.discountValue),
                                    isHighlight: true,
                                  ),
                                ],
                              ),
                            )),
            ]),
            AppSpaces.h20,
            listBuilder<PromoCodeEntity>(
              selector: (_, __) => __.promoCodeEntity,
              builder:
                  (BuildContext context, PromoCodeEntity value, Widget child) =>
                      SubPaymentItem(
                children: [
                  SpaceBetweenItem(
                      title: S.current.please_pay_total,
                      value: '\$' +
                          CashUtils.formatCashAmount(num.parse(
                                  widget.plan?.upgradePrice ??
                                      widget.plan.price) -
                              (value?.discountValue ?? 0)))
                ],
              ),
            ),
            AppSpaces.h25,
            Padding(
              padding: EdgeInsets.only(
                left: getDp(25),
                right: getDp(28),
              ),
              child: SubBillingHeaderItem(
                title: S.current.bill_to,
              ),
            ),
            AppSpaces.h10,
            listBuilder<CompanyEntity>(
              selector: (_, __) => __.currentCompany,
              builder:
                  (BuildContext context, CompanyEntity value, Widget child) =>
                      NormalBorderPop<CompanyEntity>(
                select: value,
                showShadow: true,
                height: getDp(65),
                buildDisplayName: (e) => e.name,
                horizontalPadding: getDp(25),
                group: provider.billToList,
                onSelect: provider.onChangeBillTo,
              ),
            ),
            listBuilder<bool>(
                selector: (_, __) => __.billToList.length == 0
                    ? false
                    : __.currentCompany == __.billToList.last,
                builder: (BuildContext context, bool value, Widget child) =>
                    Visibility(
                      child: child,
                      visible: value,
                    ),
                child: Column(
                  children: [
                    AppSpaces.h15,
                    InputBoxShadowField(
                      controller: provider.companyNameController,
                      focusNode: provider.companyNameFocusNode,
                      tip: S.current.company_name,
                      maxWidth: MediaQuery.of(context).size.width,
                      minWidth: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(
                        horizontal: getDp(25),
                      ),
                      height: getDp(65),
                    ),
                    AppSpaces.h15,
                    listBuilder<String>(
                      selector: (_, __) => __.companyAddress,
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              ValueShadowSelectForm(
                        title: S.current.company_address,
                        value: value,
                        onTap: provider.onChangeAddress,
                      ),
                    ),
                    AppSpaces.h15,
                    InputBoxShadowField(
                      controller: provider.unitNumberController,
                      focusNode: provider.unitNumberFocusNode,
                      tip: S.current.unit_number,
                      maxWidth: MediaQuery.of(context).size.width,
                      minWidth: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(
                        horizontal: getDp(25),
                      ),
                      height: getDp(65),
                    ),
                    AppSpaces.h15,
                    InputBoxShadowField(
                      controller: provider.postalCodeController,
                      focusNode: provider.postalCodeFocusNode,
                      tip: S.current.postal_code,
                      maxWidth: MediaQuery.of(context).size.width,
                      minWidth: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(
                        horizontal: getDp(25),
                      ),
                      height: getDp(65),
                    ),
                    AppSpaces.h15,
                    InputBoxShadowField(
                      controller: provider.contactNameController,
                      focusNode: provider.contactNameFocusNode,
                      tip: S.current.contact_name,
                      maxWidth: MediaQuery.of(context).size.width,
                      minWidth: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(
                        horizontal: getDp(25),
                      ),
                      height: getDp(65),
                    ),
                    AppSpaces.h15,
                    listBuilder<String>(
                      selector: (_, __) => __.countryCode,
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              PhoneNumberInputField(
                        defaultCountryCode: value,
                        hasIcon: false,
                        horizontalPadding: getDp(25),
                        focusNode: provider.phoneNumberFocusNode,
                        controller: provider.phoneNumberController,
                        countryCodeCallback: provider.onChangeCountryCode,
                      ),
                    ),
                    AppSpaces.h20,
                  ],
                )),
            AppSpaces.h25,
            Padding(
              padding: EdgeInsets.only(
                left: getDp(25),
                right: getDp(28),
              ),
              child: SubBillingHeaderItem(
                title: S.current.default_payment_method,
                trailing: listBuilder<bool>(
                  selector: (_, __) => __.currentSelectCard == null,
                  builder: (BuildContext context, bool value, Widget child) =>
                      value == true
                          ? Container()
                          : PressedOpacityButton(
                              onPressed: provider.onNavManager,
                              child: Text(
                                S.current.manage,
                                style: TextStyle(
                                  color: aceBlueColor,
                                  fontSize: getSp(16),
                                  fontWeight: FontWeight.w700,
                                  height: 22 / 16,
                                ),
                              ),
                            ),
                ),
              ),
            ),
            listBuilder<bool>(
              selector: (_, __) => __.currentSelectCard == null,
              builder: (BuildContext context, bool value, Widget child) =>
                  value == true
                      ? Padding(
                          padding: EdgeInsets.only(
                            top: getDp(15),
                            left: getDp(25),
                            right: getDp(28),
                          ),
                          child: PayBillItem(
                            title: S.current.add_credit_card,
                            assertPath: A.assetsImagesSettingCredit_card_1,
                            onTap: provider.onAddCreditCard,
                          ),
                        )
                      : listBuilder<SubCardsEntity>(
                          selector: (_, __) => __.currentSelectCard,
                          builder: (BuildContext context, SubCardsEntity value,
                                  Widget child) =>
                              Padding(
                            padding: EdgeInsets.only(
                              top: getDp(15),
                              left: getDp(25),
                              right: getDp(28),
                            ),
                            child: PayBillItem(
                              title: value.cardNumber,
                              assertPath: A.assetsImagesSettingCredit_card_1,
                              onTap: () => provider.onNavPay('CC', true),
                              isSelect: true,
                            ),
                          ),
                        ),
            ),
            AppSpaces.h25,
            Padding(
              padding: EdgeInsets.only(
                left: getDp(25),
                right: getDp(28),
              ),
              child: SubBillingHeaderItem(
                title: S.current.pay_using_other_method,
              ),
            ),
            listBuilder<int>(
              selector: (_, __) => __.paymentMethods.length,
              builder: (BuildContext context, int value, Widget child) =>
                  Column(
                children: List.generate(
                    value,
                    (index) => listBuilder<PaymentMethodEntity>(
                          selector: (_, __) => __.paymentMethods[index],
                          builder: (BuildContext context,
                                  PaymentMethodEntity value, Widget child) =>
                              Padding(
                            padding: EdgeInsets.only(
                              top: getDp(15),
                              left: getDp(25),
                              right: getDp(28),
                            ),
                            child: PayBillItem(
                              title: value.name,
                              assertPath: value.icon,
                              onTap: () => provider.onNavPay(value.value),
                            ),
                          ),
                        )),
              ),
            ),
            AppSpaces.h40,
          ],
        ),
      );
}
